package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/sp"
)

func (p *Player) PetListRequest(req *sp.PetListRequest) (res *sp.PetListResponse) {
	uPet := p.User.GetUserPet()
	res = &sp.PetListResponse{
		Pets: lib.FormatPetSps(uPet.Pets),
		Info: &sp.PetInfo{
			ExpNum:    uPet.ExpNum,
			FoodNum:   uPet.FoodNum,
			SelectTid: uPet.SelectTid,
		},
	}
	return
}

func (p *Player) PetSelectRequest(req *sp.PetSelectRequest) (res *sp.PetSelectResponse, err error) {
	uPet := p.User.GetUserPet()
	_, err = uPet.GetPet(req.Tid)
	if err != nil {
		res = &sp.PetSelectResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	uPet.SelectTid = req.Tid
	res = &sp.PetSelectResponse{
		Info: &sp.PetInfo{
			ExpNum:    uPet.ExpNum,
			FoodNum:   uPet.FoodNum,
			SelectTid: uPet.SelectTid,
		},
	}
	return
}

// 6003 宠物吃饲料
func (p *Player) PetFoodRequest(req *sp.PetFoodRequest) (res *sp.PetFoodResponse, err error) {

	// 成就，记录宠物累计时间，吃一个饲料加4小时，不管宠物上限。
	lib.AddAchievProgress(p.User, data.AchievementType.Wake_pet, req.Num*4)
	p.AchievementNotice()

	// 宠物吃食物
	uProp, uPet, pet, addExp, err := lib.PetEatFood(p.User, req.Tid, req.Num)
	if err != nil {
		res = &sp.PetFoodResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.PetFoodResponse{
		LessNum:   uPet.FoodNum,
		Pet:       lib.FormatPetSp(pet),
		AddExpVal: addExp,
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}

// 6004 宠物吃经验
func (p *Player) PetExpRequest(req *sp.PetExpRequest) (res *sp.PetExpResponse, err error) {
	u := p.User

	uPet := u.GetUserPet()
	if uPet.ExpNum < req.Num {
		res = &sp.PetExpResponse{
			Errcode: data.Errcode.NoPetExp,
		}
		return
	}

	pet, err := uPet.GetPet(req.Tid)
	if err != nil {
		res = &sp.PetExpResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	uProp, err := lib.AddExp(pet, req.Num)
	if err != nil {
		res = &sp.PetExpResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	uPet.ExpNum -= req.Num
	// 更新宠物信息
	uPet.Update(uPet)

	petLevel := lib.MaxPetLevel(u)
	// 成就，记录用户宠物最大等级。
	lib.AddAchievProgress(u, data.AchievementType.Level_pet, petLevel)
	// 成就，记录相同宠物等级个数
	lib.AchievDiffLevelPet(u)
	// 重新计算宠物星星
	lib.SumPetStar(u)
	// 成就，记录星星的数量
	lib.AddAchievProgress(u, data.AchievementType.Get_stars, u.StarVal())
	// 有成就完成，进行通知
	p.AchievementNotice()
	// 计算新兵特训
	lib.RecruitTrainMsg(u.Uid.Hex(), 0, u.StarSum, 0, p.Send)

	res = &sp.PetExpResponse{
		LessNum: uPet.ExpNum,
		Pet:     lib.FormatPetSp(pet),
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}

func (p *Player) PetStatusActiveRequest(req *sp.PetStatusActiveRequest) (res *sp.PetStatusActiveResponse, err error) {
	u := p.User

	uProp, err := lib.ActivePet(u, req.Tid)
	if err != nil {
		res = &sp.PetStatusActiveResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 重新计算宠物星星
	lib.SumPetStar(u)

	petSum := lib.CntPet(u)
	// 成就，记录宠物个数
	lib.AddAchievProgress(u, data.AchievementType.Kind_pet, petSum)
	// 成就，记录获取星星数量。
	lib.AddAchievProgress(u, data.AchievementType.Get_stars, u.StarVal())
	// 计算新兵特训
	lib.RecruitTrainMsg(u.Uid.Hex(), 0, u.StarSum, 0, p.Send)
	// 有成就完成，进行通知
	achLst := lib.AchievComplete(p.User)
	if len(achLst) > 0 {
		p.Send(&sp.AchievementNoticeRequest{
			Lst: achLst,
		})
	}
	if uProp != nil {
		p.TimelyUpdateProp(req.GetType(), uProp)
		utils.AsyncFunc(func() {
			p.UpdateProp(req.GetType(), uProp)
		})
	}

	res = &sp.PetStatusActiveResponse{
		Tid:    req.Tid,
		Status: 1,
	}
	return
}
